package org.example.bookstore.service.impl;

import org.apache.ibatis.annotations.Mapper;
import org.example.bookstore.custom.ResultVO;
import org.example.bookstore.mapper.CommentMapper;
import org.example.bookstore.pojo.DO.Comment;
import org.example.bookstore.pojo.DTO.CommentUser;
import org.example.bookstore.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommentServiceImpl  implements CommentService {
    @Autowired
    private CommentMapper commentMapper;

    /*
     * 评论添加
     * */
    @Override
    public ResultVO<?> addComment(Comment comment) {
        comment.setCommentLikeCount(0);
        //目前没有开发对当前评论进行评论 后期需要进行传参
        comment.setToCommentId(0);
        comment.setCommentTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        int i=commentMapper.addComment(comment);
        if (i==0){
            return ResultVO.error();
        }
        return  ResultVO.success("添加成功");
    }

    /*
     * 评论查询  根据书本id查询所有评论 list(返回用户姓名+头像+评论内容)
     * */
    @Override
    public ResultVO<?> getCommentList(Integer id) {
        List<CommentUser> commentList=commentMapper.getCommentListById(id);
        if (commentList!=null){
            return ResultVO.success("查询成功",commentList);
        }
        return ResultVO.error();
    }

    /*
     * 根据书本id评论查询总数
     * */
    @Override
    public ResultVO<?> getCommentSum(Integer id) {
        int sum=commentMapper.getCommentSumById(id);
        if (sum==0){
            return ResultVO.success("该商品没有评论");
        }
        return ResultVO.success("查询成功",sum);
    }

    /*
     * 根据评论id删除评论
     * */
    @Override
    public ResultVO<?> deleteComment(Integer id) {
        int isdelete=commentMapper.deleteCommentById(id);
        if (isdelete==0){
            return ResultVO.error();
        }
        return ResultVO.success("删除成功");
    }
}
